package com.color.admin.system.entity;


import com.color.admin.common.core.constant.Constant;
import com.color.admin.common.core.constant.ConstantData;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Set;

/**
 * @author HaiQiu
 * @date 2021/12/22
 */
@Entity
@Table(name = ConstantData.ROLE_TABLE, schema = ConstantData.SOURCE)
public class SysRole extends BaseEntity implements Serializable {

    @Schema(example = "xxx", description = "主键ID")
    @NotEmpty(message = "ID"+ Constant.NOT_EMPTY,groups = update.class)
    private String id;

    @Schema(example = "超级管理员",description = "角色名称")
    @NotEmpty(message = "名称"+ Constant.NOT_EMPTY)
    @Size(max = 20,message = "名称长度应该在1-20个字符之间")
    private String roleName;

    @Schema(description = "图标")
    @Size(max = 1000,message = "图标地址长度应该在1-1000个字符之间")
    private String icon;

    @Schema(example = "超级管理员，系统最高级权限操作", description = "描述")
    @NotEmpty(message = "描述"+ Constant.NOT_EMPTY)
    @Size(max = 500,message = "描述长度应该在1-1000个字符之间")
    private String description;

    @Schema(description = "用户关系映射", hidden = true)
    private Set<SysUser> users;

    @Schema(description = "权限菜单集合")
    private Set<SysMenu> menus;

    public void setId(String id) {
        this.id = id;
    }

    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "id")
    @GenericGenerator(name = "id", strategy = "org.hibernate.id.UUIDGenerator")
    public String getId() {
        return id;
    }

    @Column(name = "role_name",length = 20)
    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    @Column(name = "icon",length = 800)
    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    @Column(name = "description",length = 1000)
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @ManyToMany(mappedBy = "roles")
    public Set<SysUser> getUsers() {
        return users;
    }

    @JsonIgnore
    public void setUsers(Set<SysUser> userSet) {
        this.users = userSet;
    }

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = ConstantData.ROLE_MENU_TABLE,
            joinColumns = {@JoinColumn(name = "role_id")},
            inverseJoinColumns = {@JoinColumn(name = "menu_id")})
    public Set<SysMenu> getMenus() {
        return menus;
    }

    public void setMenus(Set<SysMenu> menus) {
        this.menus = menus;
    }
}
